//
//  PrinterListViewController.m
//  PrinterSDK
//
//  Created by doulai on 9/16/15.
//  Copyright (c) 2015 doulai. All rights reserved.
//

#import "PrinterListViewController.h"
#import "FormatSettingTableViewController.h"

//#import "VirturePrinter.h"
#import "PrinterSDK.h"
#import "SVProgressHUD.h"
#import <PgyUpdate/PgyUpdateManager.h>
#import <CoreBluetooth/CoreBluetooth.h>
//#import "CustomerPrintViewController.h"

@interface PrinterListViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate,BluetoothDelegate>
{
    NSArray*deviceList;
    //    NSDictionary*connetedDevice;
    NSInteger choosedIndex;
    //    VirturePrinter*printDevice;
    NSTimer* mytimer;
    UIActivityIndicatorView *activityView;
    UITableView *mytableview;
    UIButton*login;
    NSString*printerStr;
}
@end
//static NSString*printertableIdentify=@"printertableIdentify";
@implementation PrinterListViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)BackAction:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)save:(UIButton *)sender{
    //    [self.navigationController popViewControllerAnimated:YES];
    [PrinterWraper autoConnectLastPrinterTimeout:10 Completion:^(NSString *uid) {
        NSLog(@"自动连接 uid=%@",uid);
        if (uid) {
            [SVProgressHUD showSuccessWithStatus:@"自动连接成功"];
        }else
            [SVProgressHUD dismiss];
    }];
    [activityView removeFromSuperview];
    [SVProgressHUD showSuccessWithStatus:@"搜索中请稍后。。。"];
    deviceList=nil;
    [self scanPrinter:nil ];
    [mytableview reloadData];
    
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    
    UIButton*saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0,  65,35)];
    [saveBtn setTitleColor:luseColor forState:UIControlStateNormal];
    [saveBtn setTitle:@"搜索蓝牙" forState:UIControlStateNormal];
    saveBtn.titleLabel.font=[UIFont systemFontOfSize:14];
    [saveBtn addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:saveBtn];
    self.navigationItem.rightBarButtonItem = myBtnBtn;
    
    
    if ([self.shi isEqualToString:@"shida"]) {
   
    }else{
        [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:1 rotation:0];// 3 大字体  ，7默认行间距,1局中对齐
        [PrinterWraper addPrintText:@"欢迎光临\n\n"];//打印文字
        
        [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
        [PrinterWraper addPrintText:[NSString stringWithFormat: @"收银员: %@  \n", [[self.Dic objectForKey:@"order"] objectForKey:@"source"] ]];
        
        [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
        [PrinterWraper addPrintText:[NSString stringWithFormat: @"销售时间: %@  \n", [[self.Dic objectForKey:@"order"] objectForKey:@"orderDate"] ]];
        
        NSDictionary *value = [self.Dic objectForKey:@"member"];
        //           NSLog(@"---%ld",value.count);
        if (value.count==0)//零售会员
        {
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:[NSString stringWithFormat: @"会员名称: %@  \n", @"零售会员" ]];
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:[NSString stringWithFormat: @"会员卡号: %@  \n", @"--------"]];
        }else{
            
            
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:[NSString stringWithFormat: @"会员名称: %@  \n", [[self.Dic objectForKey:@"member"] objectForKey:@"memberName"] ]];
            
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            
            if (    [[Global getJudgeTheErrorStr:[[self.Dic objectForKey:@"member"] objectForKey:@"cardNo"]]isEqualToString:@""]
||[[self.Dic objectForKey:@"member"] objectForKey:@"cardNo"] ==NULL) {
                [PrinterWraper addPrintText:[NSString stringWithFormat: @"会员卡号: %@  \n", @""]];
                
            }else{
                [PrinterWraper addPrintText:[NSString stringWithFormat: @"会员卡号: %@  \n", [[self.Dic objectForKey:@"member"] objectForKey:@"cardNo"] ]];
            }
        }
        
        [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
        [PrinterWraper addPrintText:[NSString stringWithFormat: @"门店名称: %@  \n", [[self.Dic objectForKey:@"order"] objectForKey:@"companyName"] ]];
        
        [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
        [PrinterWraper addPrintText:@"--------------------------------\n\n"];
        
        //  NSArray *headers =@[@"商品名称",@"单价",@"小计"];
        NSMutableArray*MutableArray1=[[NSMutableArray alloc]init];
        
        for (NSDictionary* dicc in  [self.Dic objectForKey:@"orderItem"] )
        {
            [MutableArray1 addObject:[NSString stringWithFormat:@"%@\n",[dicc valueForKey:@"itemName"]]];
            [MutableArray1 addObject:[NSString stringWithFormat:@"%@x%@",[dicc valueForKey:@"dealPrice"],[dicc valueForKey:@"quantity"]]];
            [MutableArray1 addObject:[NSString stringWithFormat:@"   小计：%@\n",[dicc valueForKey:@"totalMoney"]]];
            
        }
        
        NSArray* body =@[MutableArray1];
        //打印商品列表，会自动排版，要求数组长度一致，空白地方用@""
        
        [PrinterWraper addItemLines:body];
        
        [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
        
        [PrinterWraper addPrintText:@"--------------------------------\n\n"];
        
        
        
        
        NSNumber*Total = [[self.Dic objectForKey:@"order"] objectForKey:@"totalMoney"];
        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
        [numberFormatter setPositiveFormat:@"###,##0.00;"];
        NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:Total.doubleValue]];
        
        NSNumber*orderPreferentia = [[self.Dic objectForKey:@"order"] objectForKey:@"mdiscountMoney"];
        NSString *formattedNumberString1 = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:orderPreferentia.doubleValue]];
        
        NSNumber*discount = [[self.Dic objectForKey:@"order"] objectForKey:@"discountMoney"];
        NSString *formattedNumberString2 = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:discount.doubleValue]];
        
        NSNumber*freight = [[self.Dic objectForKey:@"order"] objectForKey:@"expressFee"];
        NSString *formattedNumberString3 = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:freight.doubleValue]];
        
        NSNumber*preferential = [[self.Dic objectForKey:@"order"] objectForKey:@"couponMoney"];
        NSString *formattedNumberString4 = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:preferential.doubleValue]];
        
        NSNumber*realMoney = [[self.Dic objectForKey:@"order"] objectForKey:@"realMoney"];
        NSString *formattedNumberString5 = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:realMoney.doubleValue]];
        
        
        
        [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
        [PrinterWraper addPrintText:[NSString stringWithFormat: @"商品总额: %@  \n",formattedNumberString ]];
        
        
        if ([formattedNumberString1 isEqualToString:@"0.00"]) {
            
        }else{
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:[NSString stringWithFormat: @"会员优惠: %@  \n",formattedNumberString1 ]];
        }
        
        if ([formattedNumberString2 isEqualToString:@"0.00"]) {
            
        }else{
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:[NSString stringWithFormat: @"抹零折扣: %@  \n",formattedNumberString2 ]];
        }
        if ([formattedNumberString3 isEqualToString:@"0.00"]) {
            
        }else{
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:[NSString stringWithFormat: @"运费: %@  \n",formattedNumberString3 ]];
        }
        
        if ([formattedNumberString4 isEqualToString:@"0.00"]) {
            
        }else{
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:[NSString stringWithFormat: @"优惠券: %@  \n",formattedNumberString4 ]];
        }
        
        [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
        [PrinterWraper addPrintText:[NSString stringWithFormat: @"实付款: %@  \n",formattedNumberString5 ]];
        
        
        [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
        [PrinterWraper addPrintText:[NSString stringWithFormat: @"支付方式: %@  \n",[[self.Dic objectForKey:@"order"] objectForKey:@"payTypeName"] ]];
        
        [PrinterWraper setPrintFormat:1 LineSpace:58 alinment:0 rotation:0];
        [PrinterWraper addPrintText:@"--------------------------------\n\n"];
        
        [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
        [PrinterWraper addPrintText:@"谢谢光临，欢迎下次惠顾\n"];
        
        [PrinterWraper addPrintText:@"\n\n"];
   }
      self.edgesForExtendedLayout=UIRectEdgeNone;
    
    self.hidesBottomBarWhenPushed=YES;
    self.title=NSLocalizedString(@"打印机设置", @"");
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    UIButton *formatbtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width- 8-150, 0, 150, 44)];
    [formatbtn setTitle:NSLocalizedString(@"设置打印模版", @"") forState:UIControlStateNormal];
    [formatbtn addTarget:self action:@selector(openFormat:) forControlEvents:UIControlEventTouchUpInside];
    [formatbtn setBackgroundColor:[UIColor orangeColor]];
    [formatbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //[self.view addSubview:formatbtn];

    
}

-(void)jiazai{
    
    [mytableview removeFromSuperview];
    
    mytableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height- 0)];
    mytableview.delegate    =self;
    mytableview.dataSource  =self;
    mytableview.tag         =2;
    [self.view addSubview:mytableview];
    mytableview.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    [login removeFromSuperview];
    
    UIView* HeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,44*2)];
    
    
    UILabel *chooselbl=[[UILabel alloc] initWithFrame:CGRectMake(15, 0, 200, 44)];
    chooselbl.text = NSLocalizedString(@"已连接的设备", @"");
    chooselbl.font=[UIFont systemFontOfSize:16];
    chooselbl.textColor=shenhuiColor;
    [HeaderView addSubview:chooselbl];
    UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(10, 44, ScreenWidth-20, 0.40)];
    lineLabel1.backgroundColor=qianhuiColor;
    [HeaderView addSubview:lineLabel1];
    UILabel *lineLabel12=[[UILabel alloc] initWithFrame:CGRectMake(10, 44*2, ScreenWidth-20, 0.40)];
    lineLabel12.backgroundColor=qianhuiColor;
    [HeaderView addSubview:lineLabel12];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Device = [user objectForKey:@"DeviceName"];
    if (Device.length>0) {
        
        HeaderView.frame=CGRectMake(0, 0, ScreenWidth, 44*3);
        UILabel *chooselbl2=[[UILabel alloc] initWithFrame:CGRectMake(15, 44, 200, 44)];
        chooselbl2.text =Device;
        chooselbl2.font=[UIFont systemFontOfSize:15];
        chooselbl2.textColor=shenhuiColor;
        [HeaderView addSubview:chooselbl2];
        
        UILabel *chooselbl12=[[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth-200, 44, 170, 44)];
        chooselbl12.font=[UIFont systemFontOfSize:15];
        chooselbl12.textAlignment=NSTextAlignmentRight;
        
        if (deviceList.count>0 ) {
            
            chooselbl12.textColor=luseColor;
            chooselbl12.text =@"连接成功";
            login = [[UIButton alloc] initWithFrame:CGRectMake(0, ScreenHeight-45-64, ScreenWidth, 45)];
            [login setTitle:@"打印测试" forState:UIControlStateNormal];
          [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//            [login.layer setCornerRadius:3.0]; //设置矩圆角半径
//            [login.layer setBorderWidth:0.3];   //边框宽度
//            CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
//            CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){228/255.0, 228/255.0, 228/255.0, 1.0});
//            [login.layer setBorderColor:tureBTncolorref];//边框颜色
            login.backgroundColor=luseColor;
            [login addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:login];
            
        }else{
            chooselbl12.textColor=luseColor;
            chooselbl12.text =@"蓝牙未开";
            
        }
        [HeaderView addSubview:chooselbl12];
        
        
        
        UILabel *chooselbl1=[[UILabel alloc] initWithFrame:CGRectMake(15, 44*2, 200, 44)];
        chooselbl1.text = NSLocalizedString(@"搜索到的设备", @"");
        chooselbl1.font=[UIFont systemFontOfSize:15];
        chooselbl1.textColor=shenhuiColor;
        [HeaderView addSubview:chooselbl1];
        
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(10, 44*3, ScreenWidth-20, 0.40)];
        lineLabel1.backgroundColor=qianhuiColor;
        [HeaderView addSubview:lineLabel1];
        
    }else{
        
        UILabel *chooselbl1=[[UILabel alloc] initWithFrame:CGRectMake(15, 44, 200, 44)];
        chooselbl1.text = NSLocalizedString(@"搜索到的设备", @"");
        chooselbl1.font=[UIFont systemFontOfSize:15];
        chooselbl1.textColor=shenhuiColor;
        [HeaderView addSubview:chooselbl1];
        
    }
    
    mytableview.tableHeaderView = HeaderView;
    choosedIndex = -1;
}
-(void)login:(UIButton*)sender{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Device = [user objectForKey:@"Device"];
    
    if ([[NSString stringWithFormat:@"%@",[[self.Dic objectForKey:@"order"] objectForKey:@"totalMoney" ]]length]>0) {
        
    }else{
        
        self.shi=@"shida";
    }
    [PrinterWraper connectPrinter:Device shouldreset:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    printerStr=@"";
    [self scanPrinter:nil ];
    
    [mytableview reloadData];
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self stopScan];
    [PrinterWraper SetBlutoothDelegate:nil];
    
}
#pragma mark 按钮
-(void)selfprint:(id)sender
{
    //    CustomerPrintViewController *detail=[[CustomerPrintViewController alloc] init];
    //    detail.title = NSLocalizedString(@"打印自定义内容", @"");
    //    BOOL hide=  self.hidesBottomBarWhenPushed ;
    //    self.hidesBottomBarWhenPushed = YES;
    //    [self.navigationController pushViewController:detail animated:YES];
    //    self.hidesBottomBarWhenPushed = hide;
    
}
-(void)openTaobao:(id)sender
{
    ////    NSString *customURL = @"taobao://shop113684150.taobao.com/";
    //    NSString *customURL=@"http://item.taobao.com/item.htm?id=44696180568";
    //
    //    if ([[UIApplication sharedApplication]
    //         canOpenURL:[NSURL URLWithString:customURL]])
    //    {
    //        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:customURL]];
    //    }else
    //    {
    //        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://shop113684150.taobao.com"]];
    //    }
}
-(void)openFormat:(id)sender
{
    FormatSettingTableViewController *detail=[[FormatSettingTableViewController alloc] init];
    detail.title = NSLocalizedString(@"设置打印模版", @"");
    BOOL hide=  self.hidesBottomBarWhenPushed ;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detail animated:YES];
    self.hidesBottomBarWhenPushed = hide;
    
}
-(void)printTypeChanged:(UISegmentedControl*)sender
{
    
}
- (void)scanPrinter:(id)sender {
    //clean
    //    [self stopScan];
    
    [PrinterWraper SetBlutoothDelegate:self];
    [PrinterWraper StartScanTimeout:10];
    
    mytimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timeout) userInfo:nil repeats:NO];
    activityView=[[UIActivityIndicatorView alloc]     initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    
    activityView.center=self.view.center;
    
    [activityView startAnimating];
    
    [self.view addSubview:activityView];
    
    
}
-(void)stopScan{
    [PrinterWraper StopScan];
    [activityView stopAnimating];
    [mytimer invalidate];
    mytimer=nil;
}
-(void)timeout{
    [self stopScan];
    if (deviceList.count==0)
    {
        
        UIAlertView*alert=[[UIAlertView alloc] initWithTitle:nil message:NSLocalizedString(@"没有扫描到专用打印机,只支持iphone4s或者new pad及以后的苹果设备",@"") delegate:nil cancelButtonTitle:NSLocalizedString(@"确定", @"") otherButtonTitles:nil, nil];
        [alert show];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark bluetooth delegate
-(void)BlueToothOpen:(BOOL)isopen;{
    printerStr=@"";
    deviceList=nil;
    [self jiazai];
    [mytableview reloadData];
    if (!isopen) {
        [self stopScan];
        deviceList=nil;
        choosedIndex= -1;
        [mytableview reloadData];
        //                UIAlertView*alert=[[UIAlertView alloc] initWithTitle:nil message:NSLocalizedString(@"请在手机设置－》蓝牙 中打开蓝牙设备",@"") delegate:nil cancelButtonTitle:NSLocalizedString(@"确定", @"") otherButtonTitles:nil, nil];
        //                [alert show];
    }
}

-(void)updateBluetoothDevice:(NSMutableArray*)devices;
{
    deviceList=nil;
    NSLog(@"%@",deviceList);
    deviceList  =devices;
    printerStr=@"printerStr";
    [self jiazai];
    [mytableview reloadData];
}

-(void)didConnected:(NSString*)deviceUid  Result:(BOOL)success;{
    //    [SVProgressHUD dismiss];
    
    [mytableview reloadData];
    
    if (success)
        
        [self excuteTask];
    
}
-(void)finishPrint{
    //    DDLog(@"完成打印");
}
#pragma mark task
-(void)excuteTask{
    
    
    NSString*info;
    if (self.hasTask ||self.taskmodel) {
        info=NSLocalizedString(@"连接成功，立即打印单据吗",@"") ;
    }else
        
        
        if ([self.shi isEqualToString:@"shida"]) {
            info=NSLocalizedString(@"是否打印测试？",@"") ;
        }else{
            info=NSLocalizedString(@"恭喜您连接成功，是否打印票据？",@"") ;
        }
    UIAlertView*alert=[[UIAlertView alloc] initWithTitle:nil message:info delegate:self cancelButtonTitle:NSLocalizedString(@"取消", @"") otherButtonTitles:NSLocalizedString(@"确定", @""), nil];
    [alert show];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    
    if (buttonIndex==1) {
        if (self.hasTask) {
            [PrinterWraper startPrint:nil];
            self.hasTask =NO;
            //            [PrinterWraper printDictionary:self.task fromviewc:nil  printeruid:nil needPreview:NO];
            //            self.task=nil;
            [self.navigationController popViewControllerAnimated:YES];
        }else if(self.taskmodel)
        {
            [PrinterWraper printModel:self.taskmodel fromviewc:nil printeruid:nil preview:NO failed:nil];
            self.taskmodel=nil;
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            
                if ([self.shi isEqualToString:@"shida"]) {
                [PrinterWraper addPrintText:@"打印机连接成功\n打印机连接成功\n打印机连接成功\n打印机连接成功\n\n\n\n"];
                [PrinterWraper startPrint:nil];
            }else{
                
               
                [PrinterWraper startPrint:nil];
                
            }
            
        }
    }else
    {
        //dis
        //        [printDevice disconnectPrinter:nil];
    }
}
#pragma  mark table
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    return 50;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    
    return deviceList.count;
    
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"tablecellindentify"];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"tablecellindentify"];
    }
    CBPeripheral *device=[deviceList objectAtIndex:indexPath.row];
    NSString*name= device.name.length>0?device.name:NSLocalizedString(@"未知设备", @"");
    cell.textLabel.text=name;
    //    if ([self.shi isEqualToString:@"shida"]) {
    //         cell.detailTextLabel.text = NSLocalizedString(@"打印测试",@"");
    //    }else{
    //        cell.detailTextLabel.text = NSLocalizedString(@"点击连接",@"");
    //    }
    cell.detailTextLabel.font=[UIFont systemFontOfSize:15];
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    
    
    
    UIButton*jiantou = [[UIButton alloc]init];
    jiantou.frame = CGRectMake(ScreenWidth-40,10, 30, 30);
    jiantou.userInteractionEnabled=NO;
    [jiantou setImage:[UIImage imageNamed:@"shouqi"] forState:UIControlStateNormal];
    [cell.contentView addSubview:jiantou];
    
    UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(10, 50-0.40, ScreenWidth-20, 0.40)];
    lineLabel1.backgroundColor=qianhuiColor;
    [cell.contentView addSubview:lineLabel1];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    
    CBPeripheral *device=[deviceList objectAtIndex:indexPath.row];
    choosedIndex =indexPath.row;
    //  [PrinterWraper connectPrinter:device.identifier.UUIDString shouldreset:YES];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:device.identifier.UUIDString forKey:@"Device"];
    [user setObject:device.name forKey:@"DeviceName"];
    [user synchronize];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self jiazai];
    [mytableview reloadData];
    
}

@end

